package javase.collections;

import java.util.HashSet;

public class HashSetTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
	HashSet<Item> firstSet = new HashSet<Item>();
	Item firstItem = new Item(1l, "first");
	Item secondItem = new Item(2l, "second");
	
	firstSet.add(firstItem);
	firstSet.add(secondItem);
	
	System.out.println("Initial set");
	System.out.println(firstSet);
	System.out.println("");
	
	firstItem.setName("First");
	
	System.out.println("Modified set");
	System.out.println(firstSet);
	System.out.println("");
	
	System.out.println("The second set");
	HashSet<Item> secondSet = new HashSet<HashSetTest.Item>();
	secondSet.add(firstItem);
	System.out.println(secondSet);
	System.out.println("");
	
	System.out.println("Result of firstSet.removeAll(secondSet)");
	firstSet.removeAll(secondSet);
	System.out.println(firstSet);
	System.out.println("");
	
	System.out.println("Result of firstSet.removeAll(secondItem)");
	firstSet.remove(secondItem);
	System.out.println(firstSet);
	
    }

    static class Item {
	private Long id;
	private String name;
	
	@Override
	public int hashCode() {
	    final int prime = 31;
	    int result = 1;
	    result = prime * result + ((id == null) ? 0 : id.hashCode());
	    result = prime * result + ((name == null) ? 0 : name.hashCode());
	    return result;
	}
	
	@Override
	public boolean equals(Object obj) {
	    if (this == obj)
		return true;
	    if (obj == null)
		return false;
	    if (getClass() != obj.getClass())
		return false;
	    Item other = (Item) obj;
	    if (id == null) {
		if (other.id != null)
		    return false;
	    } else if (!id.equals(other.id))
		return false;
	    if (name == null) {
		if (other.name != null)
		    return false;
	    } else if (!name.equals(other.name))
		return false;
	    return true;
	}
	
	public Item(Long id, String name) {
	    super();
	    this.id = id;
	    this.name = name;
	}

	public Long getId() {
	    return id;
	}

	public void setId(Long id) {
	    this.id = id;
	}

	public String getName() {
	    return name;
	}

	public void setName(String name) {
	    this.name = name;
	}

	@Override
	public String toString() {
	    return "Item [id=" + id + ", name=" + name + "]";
	}
	
	
    }
}
